package services

import (
	"longmen/server/config/global"
	"longmen/server/pkg/db/mysql/models/models_live"
	"longmen/server/pkg/db/redis/cache"
	"strings"

	"gorm.io/gorm"
	"longmen/server/pkg/common/api_models"
)

func configURL(url string) string {
	if url == "" {
		return url
	}
	var configVideoURL = cache.GetConfigPub().VideoPlayURL
	if strings.Contains(url, "https://") || strings.Contains(url, "http://") {
		return url
	} else {
		return configVideoURL + url
	}
}

func (s *Service) VideoCategory() ([]*models_live.VideoCategory, error) {
	var res []*models_live.VideoCategory
	res = append(res, &models_live.VideoCategory{ID: 1, Title: "足球"})
	res = append(res, &models_live.VideoCategory{ID: 2, Title: "篮球"})
	res = append(res, &models_live.VideoCategory{ID: 3, Title: "娱乐"})
	res = append(res, &models_live.VideoCategory{ID: 4, Title: "电竞"})
	// res = append(res, &models.VideoCategory{ID: 5, Title: "回放"})
	res = append(res, &models_live.VideoCategory{ID: 6, Title: "其他"})
	return res, nil
}

func (s *Service) VideoSuggestion(r *api_models.VideoSuggestionReq) (*api_models.VideoSuggestionResp, error) {
	var res []*models_live.Video
	var page int
	var offset int
	var count int64
	page = r.Page
	if r.Page <= 1 {
		page = 1
	}

	if r.Size <= 0 || r.Size >= 20 {
		r.Size = 20
	}

	offset = (page - 1) * r.Size
	query := global.GetR().Model(&models_live.Video{}).Where("status=1 and replay_category=0")
	if r.TypeID != 0 {
		query.Where("type=?", r.TypeID)
	}
	err := query.Order("play_count desc").Order("rand()").Count(&count).Offset(offset).Limit(r.Size).Find(&res).Error
	if err != nil {
		return &api_models.VideoSuggestionResp{
			Total:  0,
			Videos: res,
		}, err
	}

	for idx, v := range res {
		if v.PlayURL != "" {
			res[idx].PlayURL = configURL(v.PlayURL)
		} else {
			res[idx].PlayURL = configURL(v.DownURL)
		}

	}

	return &api_models.VideoSuggestionResp{
		Total:  count,
		Videos: res,
	}, nil
}

func (s *Service) VideoRandom() ([]*models_live.Video, error) {
	var res []*models_live.Video
	err := global.GetR().Model(&models_live.Video{}).Where("status=1").Order("rand()").Limit(10).Find(&res).Error
	if err != nil {
		return res, err
	}
	for idx, v := range res {
		if v.PlayURL != "" {
			res[idx].PlayURL = configURL(v.PlayURL)
		} else {
			res[idx].PlayURL = configURL(v.DownURL)
		}
	}
	return res, nil
}

func (s *Service) VideoPlay(id int) (*models_live.Video, error) {
	var res *models_live.Video
	err := global.GetR().Model(&models_live.Video{}).Where("id=? and status=1", id).Limit(1).Find(&res).Error
	if err != nil {
		return res, err
	}
	// 更新播放量
	err = global.GetR().Model(&models_live.Video{}).Where("id=?", res.ID).Update("play_count", gorm.Expr("play_count+1")).Error
	if err != nil {
		return res, err
	}
	if res.PlayURL != "" {
		res.PlayURL = configURL(res.PlayURL)
	} else {
		res.PlayURL = configURL(res.DownURL)
	}

	return res, nil
}

func (s *Service) VideoSearch(r *api_models.VideoSearchReq) (*api_models.VideoSearchResp, error) {
	var res = make([]*models_live.Video, 0)
	var sugg = make([]*models_live.Video, 0)
	var page int
	var offset int
	var count int64
	page = r.Page
	if r.Keyword == "" {
		return &api_models.VideoSearchResp{
			Total:  0,
			Videos: res,
		}, nil
	}
	if r.Page <= 1 {
		page = 1
	}

	if r.Size <= 0 || r.Size >= 200 {
		r.Size = 200
	}

	offset = (page - 1) * r.Size

	err := global.GetR().Model(&models_live.Video{}).Where("status=1 and title like ?", "%"+r.Keyword+"%").Count(&count).Offset(offset).Limit(r.Size).Find(&res).Error
	if err != nil {
		return &api_models.VideoSearchResp{
			Total:           0,
			Videos:          res,
			VideoSuggestion: sugg,
		}, err
	}
	if len(res) > 0 {
		var typeIDs []int
		var IDs []int
		for _, v := range res {
			typeIDs = append(typeIDs, int(v.Type))
			IDs = append(IDs, v.ID)
		}

		err = global.GetR().Model(&models_live.Video{}).Where("status=1").Where("type", typeIDs).Where("id not in (?)", IDs).Order("play_count desc").Order("rand()").Limit(20).Find(&sugg).Error
		if err != nil {
			return &api_models.VideoSearchResp{
				Total:           0,
				Videos:          res,
				VideoSuggestion: sugg,
			}, err
		}
	}

	for idx, v := range res {
		if v.PlayURL != "" {
			res[idx].PlayURL = configURL(v.PlayURL)
		} else {
			res[idx].PlayURL = configURL(v.DownURL)
		}
	}
	for idx, v := range sugg {
		if v.PlayURL != "" {
			sugg[idx].PlayURL = configURL(v.PlayURL)
		} else {
			sugg[idx].PlayURL = configURL(v.DownURL)
		}
	}

	return &api_models.VideoSearchResp{
		Total:           count,
		Videos:          res,
		VideoSuggestion: sugg,
	}, nil
}

func (s *Service) VideoHot() ([]*models_live.Video, error) {
	var res []*models_live.Video
	err := global.GetR().Model(&models_live.Video{}).Where("status=1 and is_hot=1").Order("rand()").Limit(10).Find(&res).Error
	if err != nil {
		return res, err
	}
	for idx, v := range res {
		if v.PlayURL != "" {
			res[idx].PlayURL = configURL(v.PlayURL)
		} else {
			res[idx].PlayURL = configURL(v.DownURL)
		}
	}
	return res, nil
}

func (s *Service) VideoRank() ([]*models_live.Video, error) {
	var res []*models_live.Video
	err := global.GetR().Model(&models_live.Video{}).Where("status=1").Order("play_count desc").Limit(20).Find(&res).Error
	if err != nil {
		return res, err
	}
	for idx, v := range res {
		if v.PlayURL != "" {
			res[idx].PlayURL = configURL(v.PlayURL)
		} else {
			res[idx].PlayURL = configURL(v.DownURL)
		}
	}
	return res, nil
}

func (s *Service) VideoLike(r *api_models.VideoReq) (*api_models.VideoStatusResp, error) {
	err := global.GetR().Model(&models_live.Video{}).Where("id=?", r.ID).Update("like_count", gorm.Expr("like_count+1")).Error
	if err != nil {
		return &api_models.VideoStatusResp{
			Status: 0,
		}, err
	}
	return &api_models.VideoStatusResp{
		Status: 1,
	}, nil
}

func (s *Service) VideoShare(r *api_models.VideoReq) (*api_models.VideoStatusResp, error) {
	err := global.GetR().Model(&models_live.Video{}).Where("id=?", r.ID).Update("share_count", gorm.Expr("share_count+1")).Error
	if err != nil {
		return &api_models.VideoStatusResp{
			Status: 0,
		}, err
	}
	return &api_models.VideoStatusResp{
		Status: 1,
	}, nil
}

func (s *Service) VideoReplyCategory(r *api_models.VideoReq) ([]*models_live.VideoReplyCategory, error) {
	var res []*models_live.VideoReplyCategory
	err := global.GetR().Model(&models_live.VideoReplyCategory{}).Where("status=1 and parent_id=0").Order("sort desc").Find(&res).Error
	if err != nil {
		return res, err
	}
	return res, nil
}

func (s *Service) VideoReplyChildCategory(r *api_models.VideoReplyChildCategoryReq) ([]*models_live.VideoReplyCategory, error) {
	var res []*models_live.VideoReplyCategory
	err := global.GetR().Model(&models_live.VideoReplyCategory{}).Where("status=1 and parent_id=?", r.CategoryID).Order("sort desc").Find(&res).Error
	if err != nil {
		return res, err
	}
	return res, nil
}

func (s *Service) VideoReplyList(r *api_models.VideoReplyReq) (*api_models.VideoReplyResp, error) {
	var res []*models_live.Video
	var page int
	var offset int
	var count int64
	page = r.Page
	if r.Page <= 1 {
		page = 1
	}

	if r.Size <= 0 || r.Size >= 20 {
		r.Size = 20
	}

	offset = (page - 1) * r.Size

	query := global.GetR().Model(&models_live.Video{}).Where("status=1")
	if r.CategoryID > 0 {
		query.Where("replay_category=?", r.CategoryID)
	} else {
		query.Where("replay_category>0")
	}

	if r.Keyword != "" {
		query.Where("title like ?", "%"+r.Keyword+"%")
	}

	err := query.Count(&count).Offset(offset).Limit(r.Size).Order("create_time desc").Find(&res).Error
	if err != nil {
		return &api_models.VideoReplyResp{
			Total:  0,
			Videos: res,
		}, err
	}
	for idx, v := range res {
		if v.PlayURL != "" {
			res[idx].PlayURL = configURL(v.PlayURL)
		} else {
			res[idx].PlayURL = configURL(v.DownURL)
		}
	}
	return &api_models.VideoReplyResp{
		Total:  count,
		Videos: res,
	}, nil
}
